package root.electronclass.electronclass.home;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import root.electronclass.electronclass.R;
import root.electronclass.electronclass.home.widget.HomeClassView;
import root.electronclass.electronclass.home.widget.HomeCourseView;
import root.electronclass.electronclass.home.widget.HomeExcellentView;
import root.electronclass.electronclass.home.widget.HomeGoodView;
import root.electronclass.electronclass.home.widget.HomeHonorView;
import root.electronclass.electronclass.home.widget.HomeImgView;
import root.electronclass.electronclass.home.widget.HomeMsgView;
import root.electronclass.electronclass.home.widget.HomeNoticeView;
import root.electronclass.electronclass.home.widget.HomeWorkView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private ViewGroup rootView;

    private HomeClassView classView;
    private HomeCourseView courseView;
    private HomeExcellentView excellentView;
    private HomeHonorView honorView;
    private HomeImgView imgView;
    private HomeMsgView msgView;
    private HomeNoticeView noticeView;
    private HomeWorkView homeWorkView;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = (ViewGroup) inflater.inflate(R.layout.fragment_home, container, false);
        classView = new HomeClassView(getContext(), rootView.findViewById(R.id.layoutClassInfo));
        homeWorkView = new HomeWorkView(getContext(), rootView.findViewById(R.id.layoutHomeWork));
        courseView = new HomeCourseView(getContext(), rootView.findViewById(R.id.layoutCourseInfo));
        honorView = new HomeHonorView(getContext(), rootView.findViewById(R.id.layoutHonorInfo));
        excellentView = new HomeExcellentView(getContext(), rootView.findViewById(R.id.layoutExcellentInfo));
        noticeView = new HomeNoticeView(getContext(), rootView.findViewById(R.id.layoutNoticeInfo));
        imgView = new HomeImgView(getContext(), rootView.findViewById(R.id.layoutImgInfo));
        msgView = new HomeMsgView(this, rootView.findViewById(R.id.layoutMessageInfo));
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (!isAdded()) {
                            timer.cancel();
                            return;
                        }
                        classView.refresh();
                        homeWorkView.refresh();
                        courseView.refresh();
                        honorView.refresh();
                        excellentView.refresh();
                        noticeView.refresh();
                        imgView.refresh();
                        msgView.refresh();
                    }
                });
            }
        }, 10 * 60 * 1000, 10 * 60 * 1000);
        return rootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (courseView != null) {
            courseView.destroy();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (msgView != null) {
            msgView.onActivityResult(requestCode, resultCode, data);
        }
    }
}